package com.hmall.api.config;

import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 配置微服务之间数据传递
@Configuration
@ConditionalOnClass
public class DefaultFeignConfig {
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                // 获取登录用户
                Long user = UserContext.getUser();
                if (user==null){
                    // 如果用户为空则直接跳出
                    return;
                }

                // 如果不为空则放入请求头中，传递给下游微服务
                requestTemplate.header("user-info",user.toString());
            }
        };
    }
}
